import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowChoice extends JFrame implements ItemListener,ActionListener {
   JComboBox choice; 
   JTextField text;
   JTextArea area;
   JButton add,del;
   WindowChoice() {
      init();
      setSize(200,200);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
   }
   void init() {
      setLayout(new FlowLayout());
      choice=new JComboBox(); 
      text=new JTextField(8);
      area=new JTextArea(6,25);  
      choice.addItem("音乐天地"); 
      choice.addItem("武术天地");
      choice.addItem("象棋乐园");     
      choice.addItem("交友聊天");
      add=new JButton("添加");
      del=new JButton("删除");
      add.addActionListener(this);
      text.addActionListener(this);
      del.addActionListener(this);
      choice.addItemListener(this);
      add(choice);  
      add(del);
      add(text);
      add(add);
      add(new JScrollPane(area));
   }
   public void itemStateChanged(ItemEvent e) {
      String name=choice.getSelectedItem().toString();
      int index=choice.getSelectedIndex();
      area.setText("\n"+index+":"+name);
   }
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==add||e.getSource()==text) {
          String name=text.getText();
          if(name.length()>0) {
              choice.addItem(name);
              choice.setSelectedItem(name);
              area.append("\n列表添加："+name);
          }
      }
      else if(e.getSource()==del) {
          area.append("\n列表删除："+choice.getSelectedItem());
          choice.remove(choice.getSelectedIndex());
      }
   }
}
